﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.HttpContext

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:TopicSort runat=server></{0}:TopicSort>")> _
    Public Class TopicSort
        Inherits WebControl
        Private _defaultSort As String = "ASC"
        Private _forumId As Integer = -1
        Private _topicId As Integer = -1
        Protected WithEvents drpSort As DropDownList
        Public Property DefaultSort() As String
            Get
                Return _defaultSort
            End Get
            Set(ByVal value As String)
                _defaultSort = value
            End Set
        End Property
        Public Property ForumId() As Integer
            Get
                Return _forumId
            End Get
            Set(ByVal value As Integer)
                _forumId = value
            End Set
        End Property
        Public Property TopicId() As Integer
            Get
                Return _topicId
            End Get
            Set(ByVal value As Integer)
                _topicId = value
            End Set
        End Property
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            drpSort.RenderControl(writer)
        End Sub

        Private Sub TopicSort_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            'EnableViewState = False
            drpSort = New DropDownList
            drpSort.ID = "drpSort"
            drpSort.AutoPostBack = True
            drpSort.CssClass = CssClass
            drpSort.Items.Add(New ListItem("[RESX:TopicSortOldest]", "ASC"))
            drpSort.Items.Add(New ListItem("[RESX:TopicSortNewest]", "DESC"))
            Me.Controls.Add(drpSort)
        End Sub

        Private Sub drpSort_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpSort.SelectedIndexChanged
            Dim Sort As String = drpSort.SelectedItem.Value
            Dim TabId As Integer = Integer.Parse(Current.Request.QueryString("TabId"))

            Current.Response.Redirect(Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId, ParamKeys.Sort & "=" & Sort}))
        End Sub

        Private Sub TopicSort_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then


                Dim Sort As String = DefaultSort
                If Not Current.Request.Params(ParamKeys.Sort) Is Nothing Then
                    Sort = Current.Request.Params(ParamKeys.Sort)
                End If
                drpSort.SelectedIndex = drpSort.Items.IndexOf(drpSort.Items.FindByValue(Sort))
            End If
        End Sub
    End Class

End Namespace
